import axios from 'axios'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 15000 // request timeout
})

// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
  */

  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  response => {
    const res = response.data
    return res
  },
  error => {
    return Promise.reject(error)
  }
)

export default service

/**
 * Post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function Post (url, data = {},header={}) {
  return new Promise((resolve, reject) => {
    service({
      method: 'post',
      url: url,
      data: data,
      header: header
    }).then(response => {
      // 对接口错误码做处理
      resolve(response)
    }).catch(err => {
      reject(err)
    })
  })
}
/**
 * Get 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function Get (url, data = {}) {
  return new Promise((resolve, reject) => {
    service.get(url, {
      params: data
    }).then(response => {
      resolve(response)
    }).catch(err => {
      reject(err)
    })
  })
}
